package com.xgc.book.bookserver.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.code.kaptcha.Producer;
import com.xgc.book.bookserver.bean.CodeBean;
import com.xgc.book.bookserver.mapper.CodeMapper;
import com.xgc.book.bookserver.service.CodeService;
import com.xgc.book.bookserver.util.ResultJson;
import com.xgc.book.bookserver.util.exception.RTException;
import com.xgc.book.bookserver.util.tencent.SmsUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.image.BufferedImage;
import java.util.Date;
import java.util.Random;

/**
 * <p>
 * 验证码表 服务实现类
 * </p>
 *
 * @author wrz
 * @since 2023-11-20
 */
@Service
@Transactional
public class CodeServiceImpl extends ServiceImpl<CodeMapper, CodeBean> implements CodeService {

    @Autowired
    private Producer producer;

    @Autowired
    private SmsUtil smsUtil;

    @Override
    public BufferedImage createKaptcha(String uuid) {
        if (StringUtils.isEmpty(uuid)) {
            throw new RTException("uuid不能为空！");
        }
        String code = producer.createText();

        CodeBean codeBean = new CodeBean();
        codeBean.setCode(code);
        codeBean.setUuid(uuid);
        Date date = new Date();
        date.setMinutes(date.getMinutes() + 2);//设置2分钟有效
        codeBean.setExpires(date);
        this.save(codeBean);
        return producer.createImage(code);
    }

    @Override
    public boolean checkKaptcha(String uuid, String code) {
        if (StringUtils.isEmpty(uuid)) {
            throw new RTException("uuid不能为空！");
        }
        CodeBean codeBean = this.getById(uuid);//根据主键获取对象
        /*不关心验证是否成功，只要验证就直接删除该验证码*/
        this.removeById(uuid);

        return codeBean != null && codeBean.getCode().equalsIgnoreCase(code) && codeBean.getExpires().getTime() > System.currentTimeMillis();

    }

    @Override
    public ResultJson sendMsm(String phone) {
        String smsCode = getSmsCode();
        String jsonStr = smsUtil.sendMsm(phone, smsCode);
        JSONObject json = JSON.parseObject(jsonStr);
        JSONArray sendStatusSet = json.getJSONArray("SendStatusSet");
        JSONObject result = sendStatusSet.getJSONObject(0);
        String status = result.getString("Code");
        if ("Ok".equals(status)) {
            CodeBean codeBean = new CodeBean();
            codeBean.setCode(smsCode);
            codeBean.setUuid(json.getString("RequestId"));
            Date date = new Date();
            date.setMinutes(date.getMinutes() + 5);
            codeBean.setExpires(date);
            this.save(codeBean);
            return ResultJson.ok(codeBean.getUuid());
        }
        return ResultJson.error(result.getString("Message"));
    }

    private String getSmsCode() {
        String code = "";
        for (int i = 0; i < 4; i++) {
            Random random = new Random();
            int r = random.nextInt(10);
            code += r;
        }
        return code;
    }
}
